package com.xuanmiao.smsback.web.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 解决跨域访问
 * @author shenjiankang 2018/7/19 17:34
 */
@Order(Integer.MIN_VALUE)
@WebFilter
@Component
public class CrossFilter implements Filter {
    private static final Logger LOGGER = LoggerFactory.getLogger(CrossFilter.class);

    public CrossFilter() {
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        LOGGER.info("crossFiler init");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest servletRequest = (HttpServletRequest) request;
        HttpServletResponse servletResponse = (HttpServletResponse) response;
        servletResponse.setHeader("Access-Control-Allow-Origin", servletRequest.getHeader("Origin"));
        servletResponse.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE, PATCH");
        servletResponse.setHeader("Access-Control-Allow-Headers", "DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,SessionToken,Cookie,sms-uid");
        servletResponse.setHeader("Access-Control-Max-Age", "3600");
        servletResponse.setHeader("Access-Control-Allow-Credentials", "true");
        servletResponse.setHeader("Access-Control-Expose-Headers", "*");
        if ("OPTIONS".equals(servletRequest.getMethod())) {
            LOGGER.info("---------用户请求 options" + ((HttpServletRequest) request).getRequestURI());
            servletResponse.setStatus(HttpStatus.ACCEPTED.value());
        } else {
            LOGGER.info("---------用户请求" + ((HttpServletRequest) request).getRequestURI());
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {
    }
}
